import request from '@/utils/request';
import { saveAuth } from '@/utils/storage';
import { AppDispatch } from '..';

/**
 * 发送短信
 * @param mobile String
 * @returns
 */
export const sendCodes = (mobile: string) => {
  return async () => {
    const res = await request.get(`/v1_0/sms/codes/${mobile}`);
    console.log(res);
  };
};

/**
 * 登录
 * @param param0 
 * @returns 
 */
export const loginAction = ({ mobile, code }: { mobile: string, code: string }) => {
  return async () => {
    const res = await request.post(`/v1_0/authorizations`, { mobile, code });
    // console.log(res)
    saveAuth(res.data)
  }
}

/**
 * 获取用户信息
 * @returns 
 */
export const getUserInfo = () => {
  return async (dispatch: AppDispatch) => {
    const res = await request.get(`/v1_0/user`)
    // console.log(res)
    dispatch({ type: 'user/saveUser', payload: res.data })
  }
}

export const getUserProfile = () => {
  return async (dispatch: AppDispatch) => {
    const res = await request.get(`/v1_0/user/profile`)
    // console.log(res)
    dispatch({ type: 'user/saveProfile', payload: res.data })
  }
}